package com.zhonghui.wms.service.impl;

import com.zhonghui.common.utils.DateUtils;
import com.zhonghui.common.utils.SecurityUtils;
import com.zhonghui.common.utils.StringUtils;
import com.zhonghui.scm.domain.ScmPurchaseApply;
import com.zhonghui.scm.domain.ScmPurchaseApplyItem;
import com.zhonghui.scm.service.IScmPurchaseApplyService;
import com.zhonghui.wms.domain.WmsMaterial;
import com.zhonghui.wms.domain.WmsReplenishmentApplication;
import com.zhonghui.wms.domain.WmsReplenishmentApplicationDetails;
import com.zhonghui.wms.domain.vo.WmsReplenishmentApplicationVo;
import com.zhonghui.wms.mapper.WmsMaterialMapper;
import com.zhonghui.wms.mapper.WmsReplenishmentApplicationMapper;
import com.zhonghui.wms.service.IWmsReplenishmentApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 智能仓储WMS-补货申请Service业务层处理
 *
 * @author zhonghui
 * @date 2022-05-25
 */
@Service
public class WmsReplenishmentApplicationServiceImpl implements IWmsReplenishmentApplicationService
{
    @Autowired
    private WmsReplenishmentApplicationMapper wmsReplenishmentApplicationMapper;

    @Autowired
    private WmsMaterialMapper wmsMaterialMapper;

    @Autowired
    private IScmPurchaseApplyService iScmPurchaseApplyService;

    /**
     * 查询智能仓储WMS-补货申请
     *
     * @param id 智能仓储WMS-补货申请主键
     * @return 智能仓储WMS-补货申请
     */
    @Override
    public WmsReplenishmentApplication selectWmsReplenishmentApplicationById(Long id)
    {
        return wmsReplenishmentApplicationMapper.selectWmsReplenishmentApplicationById(id);
    }

    /**
     * 查询智能仓储WMS-补货申请列表
     *
     * @param wmsReplenishmentApplication 智能仓储WMS-补货申请
     * @return 智能仓储WMS-补货申请
     */
    @Override
    public List<WmsReplenishmentApplicationVo> selectWmsReplenishmentApplicationList(WmsReplenishmentApplication wmsReplenishmentApplication)
    {
        return wmsReplenishmentApplicationMapper.selectWmsReplenishmentApplicationList(wmsReplenishmentApplication);
    }

    @Override
    public List<WmsReplenishmentApplicationVo> selectWmsReplenishmentApplicationReviewList(WmsReplenishmentApplication wmsReplenishmentApplication) {
        return wmsReplenishmentApplicationMapper.selectWmsReplenishmentApplicationReviewList(wmsReplenishmentApplication);
    }

    /**
     * 新增智能仓储WMS-补货申请
     *
     * @param wmsReplenishmentApplication 智能仓储WMS-补货申请
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWmsReplenishmentApplication(WmsReplenishmentApplication wmsReplenishmentApplication)
    {
        String documentNo = generateNumber("BHSQ");
        wmsReplenishmentApplication.setDocumentNumber(documentNo);
        wmsReplenishmentApplication.setApplicant(SecurityUtils.getLoginUser().getUserId());
        wmsReplenishmentApplication.setApplicationDate(DateUtils.getNowDate());
        // 状态：0未采购1采购中2已采购
        wmsReplenishmentApplication.setStatus(Long.parseLong("0"));
        // 是否已经生成采购单 0否1是
        wmsReplenishmentApplication.setPurchaseApplyFlag(Long.parseLong("0"));
        int rows = wmsReplenishmentApplicationMapper.insertWmsReplenishmentApplication(wmsReplenishmentApplication);
        insertWmsReplenishmentApplicationDetails(wmsReplenishmentApplication);
        return rows;
    }
    private String generateNumber(String number){
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return number+format.format(date);
    }
    /**
     * 修改智能仓储WMS-补货申请
     *
     * @param wmsReplenishmentApplication 智能仓储WMS-补货申请
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWmsReplenishmentApplication(WmsReplenishmentApplication wmsReplenishmentApplication)
    {
        if (wmsReplenishmentApplication.getApprovedStatus() == null) {
            wmsReplenishmentApplicationMapper.deleteWmsReplenishmentApplicationDetailsByReplenishmentApplicationId(wmsReplenishmentApplication.getId());
            insertWmsReplenishmentApplicationDetails(wmsReplenishmentApplication);
        }
        return wmsReplenishmentApplicationMapper.updateWmsReplenishmentApplication(wmsReplenishmentApplication);
    }

    /**
     * 批量删除智能仓储WMS-补货申请
     *
     * @param ids 需要删除的智能仓储WMS-补货申请主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsReplenishmentApplicationByIds(Long[] ids)
    {
        wmsReplenishmentApplicationMapper.deleteWmsReplenishmentApplicationDetailsByReplenishmentApplicationIds(ids);
        return wmsReplenishmentApplicationMapper.deleteWmsReplenishmentApplicationByIds(ids);
    }

    /**
     * 删除智能仓储WMS-补货申请信息
     *
     * @param id 智能仓储WMS-补货申请主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsReplenishmentApplicationById(Long id)
    {
        wmsReplenishmentApplicationMapper.deleteWmsReplenishmentApplicationDetailsByReplenishmentApplicationId(id);
        return wmsReplenishmentApplicationMapper.deleteWmsReplenishmentApplicationById(id);
    }

    /**
     * 新增智能仓储WMS-补货申请明细信息
     *
     * @param wmsReplenishmentApplication 智能仓储WMS-补货申请对象
     */
    public void insertWmsReplenishmentApplicationDetails(WmsReplenishmentApplication wmsReplenishmentApplication)
    {
        List<WmsReplenishmentApplicationDetails> wmsReplenishmentApplicationDetailsList = wmsReplenishmentApplication.getWmsReplenishmentApplicationDetailsList();
        Long id = wmsReplenishmentApplication.getId();
        if (StringUtils.isNotNull(wmsReplenishmentApplicationDetailsList))
        {
            List<WmsReplenishmentApplicationDetails> list = new ArrayList<WmsReplenishmentApplicationDetails>();
            for (WmsReplenishmentApplicationDetails wmsReplenishmentApplicationDetails : wmsReplenishmentApplicationDetailsList)
            {
                wmsReplenishmentApplicationDetails.setReplenishmentApplicationId(id);
                list.add(wmsReplenishmentApplicationDetails);
            }
            if (list.size() > 0)
            {
                wmsReplenishmentApplicationMapper.batchWmsReplenishmentApplicationDetails(list);
            }
        }
    }

    @Override
    public int genPurchaseApply(Long id) {
        WmsReplenishmentApplication replenishApply = wmsReplenishmentApplicationMapper.selectWmsReplenishmentApplicationById(id);
        //  根据补货申请封装购买申请
        ScmPurchaseApply purchaseApply = new ScmPurchaseApply();
        // 采购申请类型 0计划申请1补货申请
        purchaseApply.setApplicationType(1);
        purchaseApply.setReplenishmentId(replenishApply.getId());
        purchaseApply.setReplenishmentNo(replenishApply.getDocumentNumber());
        purchaseApply.setApplicantId(replenishApply.getApplicant());
        purchaseApply.setApplicantDate(DateUtils.getNowDate());
        purchaseApply.setApplicantStatus(0);
        purchaseApply.setRemark(replenishApply.getRemark());
        List<ScmPurchaseApplyItem> applyItemList = new ArrayList<>();
        // 获取补货申请明细
        List<WmsReplenishmentApplicationDetails> detailList = replenishApply.getWmsReplenishmentApplicationDetailsList();
        for (WmsReplenishmentApplicationDetails item : detailList) {
            ScmPurchaseApplyItem detail = new ScmPurchaseApplyItem();
            detail.setMaterialId(item.getMaterialId());
            detail.setPurchaseQuantity(Integer.parseInt(String.valueOf(item.getReplenishmentNumber())));
            detail.setDemandedQuantity(Integer.parseInt(String.valueOf(item.getReplenishmentNumber())));
            detail.setRemark(item.getRemark());
            // 设置价格
            WmsMaterial materialInfo = wmsMaterialMapper.selectWmsMaterialById(item.getMaterialId());
            if (materialInfo != null) {
                detail.setMaterialPrice(materialInfo.getMaterialPrice().doubleValue());
            }
            // 根据补货申请详情列表封装购买申请详情列表
            applyItemList.add(detail);
        }
        purchaseApply.setScmPurchaseApplyItemList(applyItemList);
        int num = iScmPurchaseApplyService.add(purchaseApply);
        if (num > 0) {
            replenishApply.setId(replenishApply.getId());
            // 设置状态采购中
            replenishApply.setStatus(Long.parseLong("1"));
            // 设置已生成采购申请
            replenishApply.setPurchaseApplyFlag(Long.parseLong("1"));
            wmsReplenishmentApplicationMapper.updateWmsReplenishmentApplication(replenishApply);
        }
        return num;
    }
}
